<?php

include_once 'cls_mysql.php';
include_once 'function.php';
include_once '../util/util.php';
include_once '../conf/conf.inc';


//数据库配置
$host = GetConfInfo("db_host");		//DB主机
$dbname = GetConfInfo("db_schema"); //DB数据库名
$user = GetConfInfo("db_username"); //用户名
$pwd  = GetConfInfo("db_password"); //密码


//逻辑服务器配置
$server = GetConfInfo("server");
$port   = GetConfInfo("port");

//$path_goods_info_txt = '../../resourceV2/basic_txt/';
//$upload_dir = '../../resourceV2/photo/'
$upload_root = '../' . GetConfInfo("update_path");						//上传根目录
$upload_photp_dir = $upload_root . GetConfInfo("update_path_photo");	//上传相片路径
$path_goods_info_txt = $upload_root . GetConfInfo("update_path_txt");	//生成的商品txt路径

define('WEB_DIR', strtolower(preg_replace('/[\w]+.php/','',strtolower($_SERVER['SCRIPT_NAME']))));
define('WWW_HOST','http://'.$_SERVER['HTTP_HOST'].WEB_DIR);	
define('UPLOAD_DIR', $upload_photp_dir);


$db = new cls_mysql($host, $user, $pwd, $dbname);
if (!function_exists('json_encode')){
	$json = new Services_JSON();
	function json_encode ($arr){
		global $json;
		return $json->encode($arr);
	}
	
	function json_decode($json,$array=true)
	{
		global $json;
		$res = $json->decode($json);
		if ($array==true){
			$res = get_object_vars($res);
		}
		return $res;
	}
}

function insert($table, $data, $return_insert_id = false) 
{
	global $db;
	$sql = implodeFieldValue($data);
	$cmd = 'INSERT INTO';			
	$sql = "$cmd $table SET $sql";
	
	$return = $db->query($sql);
	return $return;
}

function implodeFieldValue($array, $glue = ',') {
		$sql = $comma = '';
		foreach ($array as $k => $v) {
			$sql .= $comma."`$k`='$v'";
			$comma = $glue;
		}
		return $sql;
	}

	
//往逻辑服务器发送消息
function socketData($server, $port, $str) {
	$commonProtocol = getprotobyname ( 'TCP' );
	$socket = socket_create ( AF_INET, SOCK_STREAM, $commonProtocol );
	socket_set_option ( $socket, SOL_SOCKET, SO_RCVTIMEO, array ("sec" => 1, "usec" => 0 ) );
	socket_connect ( $socket, $server, $port );
	$i = 0;
	$data = $str;
	if (! is_array ( $str )) {
		$str = array ($str );
	}
	for($i = 0; $i < count ( $str ); $i ++) {
		$data = $str [$i];
		$sendMethod = split ( ' ', $data );
		$sendMethod=$sendMethod[0];
		if ($data) {
			$data = pack ( 'I', strlen ( $data ) ) . $data;
			socket_write ( $socket, $data, strlen ( $data ) );
		}
		$t=time();
		while (true){
		    
		    $out = @socket_read ( $socket, 4 );
    		$arr = array ();
    		if ($out) {
    			$out2 = unpack ( "I", $out );
    			$out = @socket_read ( $socket, $out2 [1] );			
    			$rs = split ( ' ', $out );
    			$method = $rs [0];    			
    			unset ( $rs [0] );
    			$arr = json_decode ( implode ( ' ', $rs ), true );	
    			if ($method==$sendMethod)
    			break;	   
    		}
    		if (time()-$t>2){
    		    break;
    		}
    		
		}
		
		
	}
	
	socket_close ( $socket );
	return $arr;
}

	
define('THIS_PAGE', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
